import { Element } from 'diagram-js/lib/model/Types';
import { ModdleElement } from 'bpmn-moddle';
import modelerStore from '@wf/components/bpmnjs/store/modeler';
import editor from '@wf/components/bpmnjs/store/editor';
import { createModdleElement, getExtensionElementsList, addExtensionElements, removeExtensionElements } from '@wf/components/bpmnjs/utils/BpmnExtensionElementsUtil';

// 设置异步完成
export function getCompleteAsyncValue(element: Element): string | undefined {
  const prefix = editor().getProcessEngine;
  return element.businessObject[`${prefix}:completeAsync`];
}

export function setCompleteAsyncValue(element: Element, value: string) {
  const store = modelerStore();
  const modeling = store.getModeling;

  modeling.updateProperties(element, {
    [`${prefix}:completeAsync`]: value
  });
}

// 设置继承变量
export function getInheritBusinessKeyValue(element: Element): boolean | undefined {
  const prefix = editor().getProcessEngine;
  return element.businessObject.$attrs[`${prefix}:inheritBusinessKey`];
}

export function setInheritBusinessKeyValue(element: Element, value: boolean) {
  const store = modelerStore();
  const prefix = editor().getProcessEngine;
  const modeling = store.getModeling;

  modeling.updateProperties(element, {
    [`${prefix}:inheritBusinessKey`]: value
  });
}

// 设置业务键
export function getInheritVariablesValue(element: Element): boolean | undefined {
  const prefix = editor().getProcessEngine;
  return element.businessObject.$attrs[`${prefix}:inheritVariables`];
}

export function setInheritVariablesValue(element: Element, value: boolean) {
  const store = modelerStore();
  const prefix = editor().getProcessEngine;
  const modeling = store.getModeling;

  modeling.updateProperties(element, {
    [`${prefix}:inheritVariables`]: value
  });
}

// 设置实例名称
export function getProcessInstanceNameValue(element: Element): string | undefined {
  const prefix = editor().getProcessEngine;
  return element.businessObject.$attrs[`${prefix}:processInstanceName`];
}

export function setProcessInstanceNameValue(element: Element, value: string) {
  const store = modelerStore();
  const prefix = editor().getProcessEngine;
  const modeling = store.getModeling;

  modeling.updateProperties(element, {
    [`${prefix}:processInstanceName`]: value
  });
}

// 设置输入参数
export function getInParamListValue(element: Element): ModdleElement[] {
  const prefix = editor().getProcessEngine;
  const businessObject = element.businessObject;
  return getExtensionElementsList(businessObject, `${prefix}:In`);
}

export function setInParamListValue(element: Element, object: any) {
  const prefix = editor().getProcessEngine;
  const In = createModdleElement(`${prefix}:In`, object);
  addExtensionElements(element, element.businessObject, In);
}

export function removeInParamValue(element: Element, object: any) {
  removeExtensionElements(element, element.businessObject, object);
}

// 设置输出参数
export function getOutParamListValue(element: Element): ModdleElement[] {
  const prefix = editor().getProcessEngine;
  const businessObject = element.businessObject;
  return getExtensionElementsList(businessObject, `${prefix}:Out`);
}

export function setOutParamListValue(element: Element, object: any) {
  const prefix = editor().getProcessEngine;
  const Out = createModdleElement(`${prefix}:Out`, object);
  addExtensionElements(element, element.businessObject, Out);
}

export function removeOutParamValue(element: Element, object: any) {
  removeExtensionElements(element, element.businessObject, object);
}

// 设置调用流程
export function getElementCalledElementValue(element: Element): string | undefined {
  return element.businessObject['calledElement'];
}

export function setElementCalledElementValue(element: Element, value: string) {
  const store = modelerStore();
  const modeling = store.getModeling;

  modeling.updateProperties(element, {
    calledElement: value
  });
}
